package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.hospitalsystem.mapper.UserMapper;
import com.example.hospitalsystem.pojo.entity.User;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.result.ResultCodeEnum;
import com.example.hospitalsystem.service.EmailService;
import com.example.hospitalsystem.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.example.hospitalsystem.utils.EmailConstant.*;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailUtils mailUtils;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public Result<Object> getCaptchaLogin(String email) {
        if(!isEmailExist(email)){
            return Result.build(null,ResultCodeEnum.LOGIN_EMAIL_ERROR);
        }
        new Thread(()->{
            String captcha=mailUtils.getCode();
            redisTemplate.opsForValue().set(EMAIL_LOGIN_PRIM+email,captcha,5L, TimeUnit.MINUTES);
            mailUtils.sendMail(email,"您登录的验证码为:"+captcha+"五分钟之内有效","校医院系统");
        }).start();
        return Result.build(null,200,"成功发送邮件");
    }

    @Override
    public Result<Object> bindEmail(String email) {
        if(isEmailExist(email)){
            return Result.build(null,ResultCodeEnum.EMAIL_USED);
        }

        String captcha=mailUtils.getCode();
        redisTemplate.opsForValue().set(EMAIL_BINDING_EM_PRIM+email,captcha,5L,TimeUnit.MINUTES);
        mailUtils.sendMail(email,"您登录的验证码为:"+captcha+"五分钟之内有效","校医院系统");
        return Result.build(null,200,"成功发送邮件");
    }

    @Override
    public Result<Object> changePwd(String email) {
        if(!isEmailExist(email)){
            return Result.build(null,ResultCodeEnum.LOGIN_EMAIL_ERROR);
        }
        new Thread(()->{
            String captcha=mailUtils.getCode(); //EMAIL_CHANGE_PW_PRIM
            redisTemplate.opsForValue().set(EMAIL_CHANGE_PW_PRIM+email,captcha,5L,TimeUnit.MINUTES);
            mailUtils.sendMail(email,"您登录的验证码为:"+captcha+"五分钟之内有效","校医院系统");
        }).start();
        return Result.build(null,200,"成功发送邮件");
    }

    @Override
    public Result<Object> getOldCaptcha(String email) {
        if(!isEmailExist(email)){
            return Result.build(null,ResultCodeEnum.LOGIN_EMAIL_ERROR);
        }
        new Thread(()->{
            String captcha=mailUtils.getCode();
            redisTemplate.opsForValue().set(EMAIL_CHANGE_EM_OLD_PRIM+email,captcha,5L, TimeUnit.MINUTES);
            mailUtils.sendMail(email,"您的验证码为:"+captcha+"五分钟之内有效","校医院系统");
        }).start();
        return Result.build(null,200,"成功发送邮件");
    }

    @Override
    public Result<Object> getNewCaptcha(String email) {
        if(isEmailExist(email)){
            return Result.build(null,ResultCodeEnum.EMAIL_USED);
        }

        String captcha=mailUtils.getCode();
        redisTemplate.opsForValue().set(EMAIL_CHANGE_EM_NEW_PRIM+email,captcha,5L,TimeUnit.MINUTES);
        mailUtils.sendMail(email,"您绑定新邮箱的验证码为:"+captcha+"五分钟之内有效","校医院系统");
        return Result.build(null,200,"成功发送邮件");
    }


    private boolean isEmailExist(String emial){
        LambdaQueryWrapper<User> lambdaUpdateWrapper=new LambdaQueryWrapper<>();
        lambdaUpdateWrapper.eq(User::getEmail,emial);
        return userMapper.exists(lambdaUpdateWrapper);
    }

}
